$(function () {
    var page = 1;
    var pageSize = 10;
    var totalPage = 0;
    function getData() {
        $.ajax({
            type: "get",
            url: "/category/queryTopCategoryPaging",
            data: {
                page,
                pageSize
            },
            success: function (res) {
                totalPage = Math.ceil(res.total / pageSize)
                console.log(res);
                $('.catelist').html(template('cateList', res))
            }
        });
    }
    getData();
    $('#next').on('click', function () {
        page++;
        if (page > totalPage) {
            alert('已经是最后一页了')
            return
        }
        getData()
    })
    $('#prev').on('click', function () {
        page--;
        if (page < 1) {
            page = 1;
            alert('已经是第一页')
            return
        }
        getData()
    })
    $('#save').on('click', function () {
        var categoryName = $('.form-control').val().trim();
        if (!categoryName) {
            alert('请输入类别')
            $('.form-control').val('')
            return
        }
        $.ajax({
            type: "post",
            url: "/category/addTopCategory",
            data: {
                categoryName
            },
            success: function (res) {
                if (res.success) {
                    location.reload()
                } else {
                    alert(res.message)
                }
            }
        });
    })
})